<?php

class Report extends Controller
{

    public function __construct()
    {
        parent::__construct();
        if(! Identity::getInstance()->isLoggedIn()) redirect('auth');
        $this->load->model('report_model');
    }

    public function index()
    {
        $this->week();
    }

    public function week()
    {
        $this->load->view('report/index_view', array());
    }

    public function weekdata()
    {
        $data = array();

        $data['betSummary'] = $this->report_model->getBetSummary(array(
            'parent_id' => 0
        ));

        $this->load->view('report/weekdata_view', $data);
    }

    public function group($id)
    {
        $data = array();

        $data['betSummary'] = $this->report_model->getBetSummary(array(
            'parent_id' => $id
        ));

        $data['parentInfo'] = $this->report_model->getBetSummary(array(
            'id' => $id
        ));

        if(empty($data['parentInfo'])) redirect('report/weekdata');

        $this->load->view('report/group_view', $data);
    }

    public function editReport()
    {

        if (Identity::getInstance()->getRole() != '-1') {
            echo 'error';
            die();
        }

        $val = $this->input->post('ns', TRUE);
        $name = $this->input->post('name', TRUE);
        $id = $this->input->post('id', TRUE);

        if ($val != '' && $name != '' && $id != '') {

            $arrEdit = array();

            if ($name == 'nameMix') {

                $val = explode('--', $val);
                $arrEdit['acc'] = $val[0];
                $arrEdit['name'] = (isset($val[1])) ? $val[1] : '';
                $val = implode('--', $val);
            } else {
                $arrEdit[$name] = $val;
            }

            if ($this->report_model->updateBet($arrEdit, array('id' => $id))) {

                echo $val;
                die();
            }
        }
        echo 'error';
    }

    public function rm($id)
    {
        if(Identity::getInstance()->getRole() != '-1') {
            redirect('zdl');
            die();
        }

        $direct = $this->input->get('r', TRUE);

        $this->report_model->removeReport($id);

        if($direct) redirect($direct);

        redirect('report/weekdata');
    }

    public function add()
    {
        if(Identity::getInstance()->getRole() != '-1') {
            redirect('zdl');
            die();
        }

        $name = $this->input->post('name', TRUE);
        $acc = $this->input->post('acc', TRUE);
        $redirect = $this->input->post('redirect', TRUE);
        $parent = $this->input->post('parent', TRUE);

        $this->report_model->addReport(array(
            'acc' => $acc,
            'name' => $name,
            'parent_id' => $parent,
            'percent' => ($parent != 0) ? '0' : '0 / 0'
        ));

        redirect($redirect);
    }

}
